<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    :cancelText="$t('取消')"
    :okText="$t('保存')"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="24" style="color: orange; margin-bottom: 20px">
            {{ $t('说明') }}：{{ $t('为已选择的商品调价，需要调整的输入相应的兑换值即可，不需要调整的不设置。') }}
          </a-col>
          <a-col :span="12" v-if="model.coinExchangeValid">
            <a-form-model-item :label="$t('币值')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="newCoin">
              <a-input v-model="model.newCoin" oninput="value=value.replace(/^(0+)|[^\d]+/g,'')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="model.ticketExchangeValid">
            <a-form-model-item :label="$t('票值')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="newTicket">
              <a-input v-model="model.newTicket" oninput="value=value.replace(/^(0+)|[^\d]+/g,'')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="model.acoinExchangeValid">
            <a-form-model-item :label="$t('A币')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="newAcoin">
              <a-input v-model="model.newAcoin"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="model.aticketExchangeValid">
            <a-form-model-item :label="$t('A票')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="newAticket">
              <a-input v-model="model.newAticket" oninput="value=value.replace(/^(0+)|[^\d]+/g,'')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="model.pointExchangeValid">
            <a-form-model-item :label="$t('积分')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="newIntegral">
              <a-input v-model="model.newIntegral" oninput="value=value.replace(/^(0+)|[^\d]+/g,'')"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
export default {
  name: 'ChangePriceModal',
  components: {},
  data() {
    return {
      title: this.$t('批量调价'),
      width: 800,
      visible: false,
      confirmLoading: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      validatorRules: {}
    }
  },
  methods: {
    show(record) {
      let that = this
      that.visible = true
      that.model = Object.assign({}, record)
      getAction('/gift/workunits').then((res) => {
        if (res.success) {
          let dtl = {
            tenantId: Number(res.result.records[0].tenantId),
            coinExchangeValid: Number(res.result.records[0].coinExchangeValid),
            ticketExchangeValid: Number(res.result.records[0].ticketExchangeValid),
            pointExchangeValid: Number(res.result.records[0].pointExchangeValid),
            acoinExchangeValid: Number(res.result.records[0].acoinExchangeValid),
            aticketExchangeValid: Number(res.result.records[0].aticketExchangeValid),
          }
          that.model = Object.assign({}, that.model, dtl)
        } else {
          that.$message.error(res.message)
        }
      })
    },
    handleOk() {
      let that = this
      that.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = '/gift/goodsInfor/editBatch'
          let method = 'put'
          let query = Object.assign({}, that.model)
          httpAction(httpurl, query, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.visible = false
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
  },
}
</script>